#include ".\allegro\include\allegro.h"

#include "GlobalFunctions.h"
#include "Keyboard.h"
#include "CharacterEntity.h"
#include "Mouse.h"



#include <stdlib.h>


int main(void)
{

	srand ( time(NULL) );

	//select the platform library
	GlobalFunctions::Initialize();
	
	Entity* m; 

	int level = 5;
	
		m = new Entity();
		m->SetX(WIDTH/4);
		m->SetY(HEIGHT/4);
		m->SetW(25);
		m->SetH(25);
		m->SetLife(100);
		Entity::AddToEntityList(m);
	

	Entity* beam = new Entity();
	beam->SetW(6);
	beam->SetH(HEIGHT - m->GetY() - 50);
	beam->SetLife(0);
	
	
	int energy = 0;
	int max_energy = 50;


	

	for(int i = 0; i < 10; i++){
		Entity* enemy = new Entity();
		enemy->SetX(rand()%WIDTH/2);
		enemy->SetY(rand()%HEIGHT/2);
		enemy->SetW(25);
		enemy->SetH(25);
		enemy->SetLife(100);
		Entity::AddToEntityList(enemy);
	}


    
    Vector2D mouse_coordinates;
		
	//input for players positions
	while(LibraryInterface::GetApplicationState() != QUIT){
		if(key[KEY_ESC])
			LibraryInterface::SetApplicationState(QUIT);
		
		

		//perform pre-update operations (necessary for timing)
		GlobalFunctions::PreOnFrameUpdate();

		//allow for pausing the application
		if(LibraryInterface::GetApplicationState() == RUN){

			////map.Blit();
			for(int i = 0; i < Entity::GetEntityCount(); i++)
				Entity::GetEntity(i)->GetBody()->colliding = false;
			
			
			int font_size = 12;
			int line = 0;

			LibraryInterface::Print("bx = ", 10, line+=font_size);
			LibraryInterface::Print(beam->GetX(), 50, line);
			LibraryInterface::Print("by = ", 10, line+=font_size);
			LibraryInterface::Print(beam->GetY(), 50, line);
			LibraryInterface::Print("energy: ", 10, line+=font_size);
			LibraryInterface::Print(m->GetBody()->colliding, 70, line);

			//LibraryInterface::Print(Keyboard::GetKey(K_SPACE), 0, line+=font_size);

			float speed = 1.25;
			if(Keyboard::GetKey(K_UP))
				m->ApplyForce(0,-speed);
			if(Keyboard::GetKey(K_DOWN))
				m->ApplyForce(0,speed);
			if(Keyboard::GetKey(K_LEFT))
				m->ApplyForce(-speed,0);
			if(Keyboard::GetKey(K_RIGHT))
				m->ApplyForce(speed,0);

			//shoot
			if(Keyboard::GetKey(K_SPACE)){
				if(energy > 0){
					beam->SetX(m->GetX());
					beam->SetY(m->GetY() - beam->GetH()/2 - 10);
					beam->SetH(m->GetY()-20);
					beam->Update();
					beam->Blit();
					energy--;
					
					for(int i = 0; i < Entity::GetEntityCount(); i++)
						beam->CollideWith(Entity::GetEntity(i));		
				}
			}
			else{
				if(energy < max_energy)
					energy++;
			}

			//m->SetChanged(true);

			Entity::UpdateAllEntities();

            LibraryInterface::Print((int)Entity::GetEntityCount(), WIDTH-20, 100);			

            LibraryInterface::PrintDebugInfo();
		
		}
		
        
		

        //TODO: move PostOnFrameUpdate to GlobalData
		//perform post-update operations (necessary for timing)
		GlobalFunctions::PostOnFrameUpdate();

		
 
	}

	delete m;
	delete beam;
	
	//deinitialize routines for the platform library
	GlobalFunctions::Deinitialize();

	//end program
	return 0;
}
END_OF_MAIN()
